package jltx.fragmentpageframework.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.jltx.utils.ViewHelper;
import jltx.fragmentpageframework.R;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/06/21.
 */

public class BottomBarTab extends FrameLayout{
    private ChangeColorImageView mIcon;
    private ChangeColorTextView mTvTitle;
    private TextView mTvUnreadCount;
    private int mTabPosition = -1;

    private int selectMskColor = -1;
    private int unSelectMskColor = -1;
    private boolean showBottomText;
    private float offset=0;

    public BottomBarTab(@NonNull Context context) {
        super(context);
        initView(context,null);
    }

    public BottomBarTab(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context,attrs);
    }

    public BottomBarTab(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context,attrs);
    }

    public void initView(Context context,AttributeSet attrs){

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ChangeColorView);
        int n = a.getIndexCount();

        for (int i=0; i < n; i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.ChangeColorView_unSelectMaskColor:
                    unSelectMskColor = a.getColor(attr,unSelectMskColor);
                    break;
                case R.styleable.ChangeColorView_selectMaskColor:
                    selectMskColor = a.getColor(attr,selectMskColor);
                    break;
                case R.styleable.ChangeColorView_showBottomText:
                    showBottomText = a.getBoolean(attr,false);
                    break;

            }
        }
        a.recycle();

        LayoutInflater.from(context).inflate(R.layout.bottom_tab,this);
        TypedValue typedValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
        int[] attribute = new int[]{android.R.attr.selectableItemBackground};
        TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(typedValue.resourceId, attribute);
        this.setBackground(typedArray.getDrawable(0));

        typedArray.recycle();
        mIcon = ViewHelper.findViewById(this,R.id.bottom_tab_icon);
        mTvTitle = ViewHelper.findViewById(this,R.id.bottom_tab_tv);
        mTvUnreadCount = ViewHelper.findViewById(this,R.id.bottom_tab_unread_tv);
        mTvUnreadCount.setVisibility(GONE);

        if(unSelectMskColor != -1 && selectMskColor != -1){
            setIconMaskAndTextColor(unSelectMskColor,selectMskColor);
        }

        setShowBottomText(showBottomText);


    }

    public BottomBarTab setIconMaskAndTextColor(@ColorInt int unselectColor, @ColorInt int selectColor) {
        mIcon.setIconMaskColor(unselectColor,selectColor);
        mTvTitle.setIconBottomTextColor(unselectColor,selectColor);
        return this;
    }

    public BottomBarTab setIconMaskAndTextColorRes(@ColorRes int unselectColor, @ColorRes int selectColor) {
        mIcon.setIconMaskColorRes(unselectColor,selectColor);
        mTvTitle.setIconBottomTextColorRes(unselectColor,selectColor);
        return this;
    }

    public BottomBarTab setIconImageResource(@DrawableRes int resId) {
        mIcon.setImageResource(resId);
        return this;
    }

    public BottomBarTab setIconImageWidthAndHeight(@DimenRes int width, @DimenRes int height) {
        int widthPx = getResources().getDimensionPixelSize(width);
        int heightPx = getResources().getDimensionPixelSize(height);
        if(widthPx > 0 && heightPx > 0){
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)mIcon.getLayoutParams();
            layoutParams.width = getResources().getDimensionPixelSize(width);
            layoutParams.height = getResources().getDimensionPixelSize(height);
            mIcon.setLayoutParams(layoutParams);
        }
        return this;
    }

        public BottomBarTab setShowBottomText(boolean isShow){
        this.showBottomText = isShow;
        if(isShow){
            mTvTitle.setVisibility(View.VISIBLE);
        }else {
            mTvTitle.setVisibility(View.GONE);
        }
        return this;
    }

    public  BottomBarTab setBottomText(CharSequence text) {
        mTvTitle.setBottomText(text);
        return this;
    }


    public void changeBottomBarTabColor(float offset){
        this.offset = offset;
        mIcon.changeImageViewDrawable(offset);
        mTvTitle.changeImageViewDrawable(offset);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            changeBottomBarTabColor(1f);
        } else {
            changeBottomBarTabColor(0f);
        }
    }

    /**
     * 设置未读数量
     */
    public void setUnreadCount(int num) {
        if (num <= 0) {
            mTvUnreadCount.setText(String.valueOf(0));
            mTvUnreadCount.setVisibility(GONE);
        } else {
            mTvUnreadCount.setVisibility(VISIBLE);
            if (num > 99) {
                mTvUnreadCount.setText("99+");
            } else {
                mTvUnreadCount.setText(String.valueOf(num));
            }
        }
    }

    public void setTabPosition(int position) {
        mTabPosition = position;
    }

    public int getTabPosition() {
        return mTabPosition;
    }

}
